﻿using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DSPL.Reports.Classes.Category
{
    abstract class CategoryReports
    {
        #region Declarations
        SqlConnection sqlConn = null;
        #endregion

        #region Properties
        public List<IReportParameters> ParameterValueList = new List<IReportParameters>();

        #endregion

        #region Fields
        public string documentId;
        public ReportDocument reportDoc;
        #endregion

        #region Abstract methods
        public abstract ReportDocument GetReportDocument(DataTable reportDataTable = null);
        #endregion

        #region Cond-Dis
        public CategoryReports()
        {
            try
            {
                //ResolveDependencies();
                //DatabaseAccess.OpenConnection();
                //sqlConn = DatabaseAccess.DatabaseConnection;
                sqlConn = new SqlConnection(GlobalVariables.Connectionstring);
                sqlConn.Open();
            }
            catch (Exception)
            {
                throw;
            }
        }

       

        public void Dispose()
        {
            try
            {
                sqlConn.Dispose();
            }
            catch (Exception)
            {
                throw;
            }

        }

        #endregion

        #region Function
        /// <summary>
        /// Generate the report using connection information
        /// </summary>
        /// <param name="catReport">Name of the report</param>
        /// <returns>The created report to view</returns>
        public ReportDocument GetFilledReport(ReportDocument catReport, DataTable reportDataTable= null)
        {
            try
            {
                if (sqlConn.State == ConnectionState.Open)
                {

                    ConnectionInfo crConnectionInfo = new ConnectionInfo();
                    crConnectionInfo.ServerName = sqlConn.DataSource;
                    crConnectionInfo.DatabaseName = sqlConn.Database;
                    crConnectionInfo.UserID = "sa";
                    crConnectionInfo.Password = "data$123";

                    reportDoc = catReport;

                    if (reportDataTable != null)                   
                        reportDoc.SetDataSource(reportDataTable);

                    foreach (Table table in reportDoc.Database.Tables)
                    {
                        TableLogOnInfo crTableLogonInfo = new TableLogOnInfo();
                        crTableLogonInfo.TableName = table.Name;
                        crTableLogonInfo.ConnectionInfo = crConnectionInfo;
                        table.ApplyLogOnInfo(crTableLogonInfo);
                    }

                    if (ParameterValueList != null && ParameterValueList.Count > 0)
                    {
                        foreach (IReportParameters ivs in ParameterValueList)
                        {
                            if (ivs.ParameterName.Trim().Length <= 0)
                            {
                                throw new Exception("Parameter name not assigned" + "\n@ " + this.ToString());
                            }
                            
                            //need the order
                            if (ivs.SubReport == null)
                            {
                                if (ivs.InputValueRange != null && ivs.InputValueRange.Length > 0)
                                {
                                    ParameterValues pvs = new ParameterValues();

                                    ParameterRangeValue paramRangeValue = new ParameterRangeValue();
                                    paramRangeValue.StartValue = ivs.InputValueRange[0];
                                    paramRangeValue.EndValue = ivs.InputValueRange[ivs.InputValueRange.Length - 1];
                                    pvs.Add(paramRangeValue);

                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), pvs);
                                }
                                else
                                {
                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), ivs.InputValue);
                                }
                            }
                            else if (ivs.SubReport.Trim().Length <= 0)
                            {
                                if (ivs.InputValueRange != null && ivs.InputValueRange.Length > 0)
                                {
                                    ParameterValues pvs = new ParameterValues();

                                    ParameterRangeValue paramRangeValue = new ParameterRangeValue();
                                    paramRangeValue.StartValue = ivs.InputValueRange[0];
                                    paramRangeValue.EndValue = ivs.InputValueRange[ivs.InputValueRange.Length - 1];
                                    pvs.Add(paramRangeValue);

                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), pvs);
                                }
                                else
                                {
                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), ivs.InputValue);
                                }
                            }
                            else
                            {
                                if (ivs.InputValueRange != null && ivs.InputValueRange.Length > 0)
                                {
                                    ParameterValues pvs = new ParameterValues();

                                    ParameterRangeValue paramRangeValue = new ParameterRangeValue();
                                    paramRangeValue.StartValue = ivs.InputValueRange[0];
                                    paramRangeValue.EndValue = ivs.InputValueRange[ivs.InputValueRange.Length - 1];
                                    pvs.Add(paramRangeValue);

                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), pvs, ivs.SubReport);
                                }
                                else
                                {
                                    reportDoc.SetParameterValue(ivs.ParameterName.ToString(), ivs.InputValue, ivs.SubReport);
                                }

                            }
                        }
                    }
                    return reportDoc;

                }
                else
                {
                    throw new Exception("Database connection is broken");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }




        #endregion

    }
}
